package com.chenfan.filestore.controller;

import com.chenfan.ccp.common.result.R;
import com.chenfan.filestore.dto.WordToPdfDTO;
import com.chenfan.filestore.service.FileService;
import com.chenfan.filestore.vo.FileResult;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 文件上传下载服务
 *
 * @author SXR
 * @date 2018/7/12
 * @see #upload(MultipartHttpServletRequest) 上传文件接口
 * @see #download(String, HttpServletRequest, HttpServletResponse) 下载文件接口
 * @see #viewImg(String, HttpServletRequest, HttpServletResponse) 查看图片接口
 * @see #downloadToZip(String, String, HttpServletRequest, HttpServletResponse) 批量下载压缩文件接口
 */
@RestController
@RequestMapping("file/")
public class FileController {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private FileService fileService;
//
//    @Autowired
//    private PdfService pdfService;

    @ApiOperation(value = "上传文件", notes = "上传文件")
    @PostMapping(value = "upload")
    public R<Object> upload(MultipartHttpServletRequest request) {
        LOGGER.info("进入方法>>>--upload--上传文件");
        if (null != request) {
            List<FileResult> fileResults = null;
            try {
                fileResults = fileService.uploadFiles(request);
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
                return R.fail(e.getMessage());
            }
            LOGGER.info("退出方法>>>--upload--上传文件");
            return R.data(fileResults);
        }
        LOGGER.info("退出方法>>>--upload--上传文件");
        return R.fail("上传文件不能为空");
    }

    @ApiOperation(value = "上传文件", notes = "上传文件")
    @PostMapping(value = "upload/force")
    public R<Object> uploadForce(MultipartHttpServletRequest request, String bucketName,@RequestParam String filePath) {
        LOGGER.info("进入方法>>>--upload--上传文件");
        if (null != request) {
            List<FileResult> upload = null;
            upload = fileService.uploadFiles(request, bucketName, filePath);
            LOGGER.info("退出方法>>>--upload--上传文件");
            return R.data(upload);
        }
        LOGGER.info("退出方法>>>--upload--上传文件");
        return R.fail("上传文件不能为空");
    }

    @ApiOperation(value = "上传多个文件文件", notes = "上传多个文件文件")
    @PostMapping(value = "upload/batch")
    public R<Object> uploadBatch(MultipartHttpServletRequest request) {
        LOGGER.info("进入方法>>>--upload--上传多个文件文件");
        if (null != request) {
            List<FileResult> upload = null;
            try {
                upload = fileService.uploadFiles(request);
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
                return R.fail(e.getMessage());
            }
            LOGGER.info("退出方法>>>--upload--上传多个文件文件");
            return R.data(upload);
        }
        LOGGER.info("退出方法>>>--upload--上传多个文件文件");
        return R.fail("上传文件不能为空");
    }

    @ApiOperation(value = "下载文件", notes = "下载文件")
    @GetMapping(value = "download")
    public R<Object> download(String fileId, HttpServletRequest request, HttpServletResponse response) {
        LOGGER.info("进入方法>>>--download--下载文件 filePath=" + fileId);
        if (StringUtils.isBlank(fileId)) {
            return R.fail("文件ID不能为空");
        }
        try {
            FileResult result = new FileResult();
            result.setId(fileId);
            fileService.download(result, request, response);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return R.fail(e.getMessage());
        }
        LOGGER.info("退出方法>>>--download--下载文件");
        return R.ok();
    }

    @ApiOperation(value = "读取图片", notes = "读取图片")
    @GetMapping(value = "view/{id}")
    public void viewImg(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) {
        LOGGER.info("进入方法>>>--view_img--读取图片 id=" + id);

        if (id != null && !"null".equalsIgnoreCase(id)) {
            try {
                fileService.viewImg(id, request, response);
            } catch (Exception e) {
                LOGGER.error("读取图片文件失败", e.getMessage(), e);
                LOGGER.info("退出方法>>>--view_img--读取图片");
            }
            LOGGER.info("退出方法>>>--view_img--读取图片");
        } else {
            LOGGER.info("退出方法>>>--viewsImg--读取图片>没有文件id");
        }
    }

    @ApiOperation(value = "读取图片", notes = "读取图片")
    @GetMapping(value = "views/{id}")
    public void viewsImg(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) {
        LOGGER.info("进入方法>>>--viewsImg--读取图片 id=" + id);
        if (id != null && !"null".equalsIgnoreCase(id)) {
            request.setAttribute("resize", 2);
            try {
                fileService.viewImg(id, request, response);
            } catch (Exception e) {
                LOGGER.error("读取图片文件失败", e.getMessage(), e);
                LOGGER.info("退出方法>>>--viewsImg--读取图片");
            }
            LOGGER.info("退出方法>>>--viewsImg--读取图片");
        } else {
            LOGGER.info("退出方法>>>--viewsImg--读取图片>没有文件id");
        }
    }

    @Deprecated
    @ApiOperation(value = "批量下载压缩文件", notes = "批量下载压缩文件")
    @GetMapping(value = "downloadToZip")
    public R<Object> downloadToZip(
            String zipName,
            String fileIds,
            HttpServletRequest request,
            HttpServletResponse response) {
        LOGGER.info("进入方法>>>--downloadToZip--批量下载压缩文件 ids=" + fileIds);
        if (StringUtils.isBlank(fileIds)) {
            return R.fail("文件ID不能为空");
        }
        try {
            String[] fileIdsArray = fileIds.split("\\;");
            fileService.downloadToZip(zipName, fileIdsArray, request, response);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            LOGGER.info("退出方法>>>--downloadToZip--批量下载压缩文件");
            return R.fail(e.getMessage());
        }
        LOGGER.info("退出方法>>>--downloadToZip--批量下载压缩文件");
        return R.ok();
    }

    @PutMapping("/to-word")
    public R<String> toWord(@RequestBody WordToPdfDTO wordToPdf) throws IOException {
        return R.data(fileService.toWord(wordToPdf));
    }

    @PutMapping("/word-to-pdf")
    public R<String> wordToPdf(@RequestBody WordToPdfDTO wordToPdf) {
        return R.data(fileService.wordToPdf(wordToPdf));
    }

}
